package com.animee.jiujiangdict;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.GridView;
import com.animee.jiujiangdict.adapter.BushouListAdapter;
import com.animee.jiujiangdict.bean.BuShouBean;
import com.animee.jiujiangdict.bean.PinyinWordBean;
import com.animee.jiujiangdict.db.DBManager;
import com.animee.jiujiangdict.utils.AssetsUtils;
import com.animee.jiujiangdict.utils.URLUtils;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import java.util.ArrayList;
import java.util.List;
public class SearchBuShouActivity extends BaseSearchActivity {
    List<String>groupDatas;
    List<List<BuShouBean.ResultBean>>childDatas;
    BushouListAdapter listAdapter;

    String url ;   //获取拼音汉字的网址
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        word = "丨";
        titleTv.setText(R.string.main_tv_bushou);
        setExLvListener();
        url = URLUtils.getBushouurl(word,page,pagesize);
        loadData(url);

    }
    @Override
    public void setGVListener() {
        super.setGVListener();
        //设置上拉加载
        refreshGv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>() {

            @Override
            public void onRefresh(PullToRefreshBase<GridView> refreshView) {
                // 上拉加载的监听  在此处完成分页
                if (page<totalpage) {  //当前加载页数小于最大页数，说明还有数据，还能加载
                    page++;
                    url = URLUtils.getBushouurl(word,page,pagesize);
                    loadData(url);
                }else {
                    refreshGv.onRefreshComplete();
                }
            }
        });
    }
    /*
     * 改变拼音，从网上获取对应的汉字的方法
     * */
    private void getDataAlterWord() {
        List<BuShouBean.ResultBean> groupList = childDatas.get(selGroupPos);
        if (groupList.size()>selChildPos) {
            BuShouBean.ResultBean bean = groupList.get(selChildPos);
            word = bean.getBushou();
            page = 1;
            //加载网络数据
            url = URLUtils.getBushouurl(word,page,pagesize);
            loadData(url);
        }
    }
    /** 设置ExpandListView的监听事件*/
    private void setExLvListener() {
        // 点击了分组的内容
        exLv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                listAdapter.setSelectGroupPos(groupPosition);
                listAdapter.notifyDataSetInvalidated();     //提示重新绘制
                // 获取被点击的位置的内容
                selGroupPos = groupPosition;
                getDataAlterWord();
                return false;
            }
        });

        // 点击每组当中对应选项的内容
        exLv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                listAdapter.setSelectGroupPos(groupPosition);
                listAdapter.setSelectChildPos(childPosition);
                listAdapter.notifyDataSetInvalidated();
                selGroupPos = groupPosition;
                selChildPos = childPosition;
                getDataAlterWord();
                return false;
            }
        });
    }
    /* 初始化数据：显示在ExpandListView和PullToRefreshGridView当中的*/
    @Override
    public void initData() {
        super.initData();
        groupDatas = new ArrayList<>();
        childDatas = new ArrayList<>();
        String json = AssetsUtils.getAssetsContent(this, "bushoulist.txt");
        // 解析json数据
        BuShouBean bsBean = new Gson().fromJson(json, BuShouBean.class);
        List<BuShouBean.ResultBean> list = bsBean.getResult();
        List<BuShouBean.ResultBean> templist=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BuShouBean.ResultBean bean = list.get(i);
            String bihua = bean.getBihua();
            if (groupDatas.contains(bihua)) {
                templist.add(bean);
            }else{
                groupDatas.add(bihua);
                if (templist.size()>0) {
                    childDatas.add(templist);
                }
                templist = new ArrayList<>();
                templist.add(bean);
            }
        }
        childDatas.add(templist);
        listAdapter = new BushouListAdapter(this, groupDatas, childDatas);
        exLv.setAdapter(listAdapter);
    }
    /** 获取网络数据失败时，回调用的方法*/
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        List<PinyinWordBean.ResultBean.ListBean> list = DBManager.queryBsWordFromPywordtb(word, page, pagesize);
        refreshDataByGV(list);
    }

}
